# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.Qt import (QThread, QMutex, pyqtSignal)

from PyQt5 import QtCore, QtWidgets
import time
from tran_main import run_run_run

class Ui_main_window(object):
    def __init__(self):
        self.setFixedSize(300, 170)
        self.directory = ([], "")

    def setupUi(self, main_window):
        main_window.setObjectName("main_window")
        main_window.resize(300, 170)
        main_window.setStyleSheet("font: 9pt \"微软雅黑\";")
        self.selectFile = QtWidgets.QPushButton(main_window)
        self.selectFile.setGeometry(QtCore.QRect(10, 50, 281, 31))
        self.selectFile.setObjectName("selectFile")
        self.selectFile.clicked.connect(self.file_select)
        self.input_form_lang = QtWidgets.QComboBox(main_window)
        self.input_form_lang.setGeometry(QtCore.QRect(10, 10, 121, 31))
        self.input_form_lang.setObjectName("input_form_lang")
        self.input_form_lang.addItem("")
        self.input_form_lang.addItem("")
        self.input_form_lang.addItem("")
        self.input_form_lang.addItem("")
        self.input_form_lang.addItem("")
        self.input_to_lang = QtWidgets.QComboBox(main_window)
        self.input_to_lang.setGeometry(QtCore.QRect(170, 10, 121, 31))
        self.input_to_lang.setObjectName("input_to_lang")
        self.input_to_lang.addItem("")
        self.input_to_lang.addItem("")
        self.input_to_lang.addItem("")
        self.input_to_lang.addItem("")
        self.input_to_lang.addItem("")
        self.run_button = QtWidgets.QPushButton(main_window)
        self.run_button.setGeometry(QtCore.QRect(10, 90, 281, 31))
        self.run_button.setObjectName("run_button")
        self.run_button.clicked.connect(self.run_btn_clicked)
        self.point = QtWidgets.QLabel(main_window)
        self.point.setGeometry(QtCore.QRect(140, 15, 21, 21))
        self.point.setStyleSheet("color:#2469A4 ;font: 75 14pt \"微软雅黑\";")
        self.point.setObjectName("point")

        self.result_text = QtWidgets.QLabel(main_window)
        self.result_text.setGeometry(QtCore.QRect(10, 120, 281, 50))
        self.result_text.setStyleSheet("color:#2469A4 ;font: 75 9pt \"微软雅黑\";")
        self.result_text.setObjectName("result_text")

        self.retranslateUi(main_window)
        QtCore.QMetaObject.connectSlotsByName(main_window)

    def retranslateUi(self, main_window):
        _translate = QtCore.QCoreApplication.translate
        main_window.setWindowTitle(_translate("main_window", "文本翻译"))
        self.selectFile.setText(_translate("main_window", "选择文件"))
        self.input_to_lang.setItemText(0, _translate("main_window", "中文(简体)", "zh"))
        self.input_to_lang.setItemText(1, _translate("main_window", "英语", "en"))
        self.input_to_lang.setItemText(2, _translate("main_window", "日语", "jp"))
        self.input_to_lang.setItemText(3, _translate("main_window", "中文(文言文)", "wyw"))
        self.input_to_lang.setItemText(4, _translate("main_window", "中文(繁体)", "cht"))
        self.input_form_lang.setItemText(0, _translate("main_window", "中文(简体)", "zh"))
        self.input_form_lang.setItemText(1, _translate("main_window", "英语", "en"))
        self.input_form_lang.setItemText(2, _translate("main_window", "日语", "jp"))
        self.input_form_lang.setItemText(3, _translate("main_window", "中文(文言文)", "wyw"))
        self.input_form_lang.setItemText(4, _translate("main_window", "中文(繁体)", "cht"))
        self.input_to_lang.setCurrentIndex(0)
        self.input_form_lang.setCurrentIndex(2)
        self.run_button.setText(_translate("main_window", "开始翻译"))
        self.point.setText(_translate("main_window", "→"))
        self.result_text.setText(_translate("main_window", "请选择语言与文件后点击开始翻译"))

    def file_select(self):
        # 当窗口非继承QtWidgets.QDialog时，self需替换成 None
        directory = QtWidgets.QFileDialog.getOpenFileNames(None, None, "", "Text Files (*.txt)", "")
        self.directory = directory
        self.selectFile.setText("已选取%s个文件" % len(directory[0]))

    def run_btn_clicked(self):
        lang_list = ["zh", "en", "jp", "wyw", "cht"]
        try:
            # 禁用按钮
            self.run_button.setEnabled(False)
            self.result_text.setText("正在进行翻译，请稍后...")

            from_lang = lang_list[self.input_form_lang.currentIndex()]
            to_lang = lang_list[self.input_to_lang.currentIndex()]

            self.thread_run = Thread_1(from_lang, to_lang,self.directory[0])
            self.thread_run._signal.connect(self.finish_run)
            self.thread_run.start()


        except Exception:
            print(Exception.args)
    # 运行结束以后执行的方法
    def finish_run(self):
        self.run_button.setEnabled(True)
        self.result_text.setText("翻译完成...")
        QtWidgets.QMessageBox.about(self, "提示", "翻译完成")

class MyWindow(QMainWindow, Ui_main_window):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.setupUi(self)


qmut_1 = QMutex()  # 创建线程锁
class Thread_1(QThread):  # 线程1
    _signal = pyqtSignal()

    def __init__(self, from_long, to_long,file_list):
        super().__init__()
        self.from_long = from_long
        self.to_long = to_long
        self.file_list = file_list

    def run(self):
        # qmut_1.lock()  # 加锁
        print(self.from_long, self.to_long,self.file_list)
        run_run_run(self.from_long,self.to_long,self.file_list)
        # qmut_1.unlock()  # 解锁
        time.sleep(2)
        self._signal.emit()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyWindow()
    myWin.show()
    sys.exit(app.exec_())
